/** 
 *	@file:  databasemonitor_test.cpp тестирование операций DatabaseMonitor
 *	@author	Казаков А.В.
 *	@date	21.08.2012
 */

#include <QtGui/QApplication>
#include <stdlib.h>
#include <iostream>
#include "../databasemonitor.h"
#include "../literaturecard.h"
#include <QString>
#include <QWidget>
#include <list>

/**
 * Simple C++ Test Suite
 */

/**	имя базы данных	*/
QString qstrDataBaseName = "sql.sqlite";

/**	тест создания базы данных	*/
void testCreateDataBase()
{
	bool	bResult = true;
	QString result;
	DatabaseMonitor databaseMonitor;
	try
	{
		result = databaseMonitor.createDataBase(qstrDataBaseName);

		if(result != qstrDataBaseName)
		{
			std::cout << "user decline remove"
				<< std::endl;
			bResult = false;
		}
		else
			bResult = true;
	}
	catch(int	nEx)
	{
		if(nEx == CANT_OPEN_DATABASE)
			std::cout << "DB not exist" << std::endl;
		
		bResult = false;
	}
	catch(...)
	{
		bResult = false;
	}
	
	
	if(!bResult)
	{
		std::cout << "%TEST_FAILED% time=0 testname=testCreateDataBase "
			"(databasemonitor_test) message=error message sample" 
			<< std::endl;
	}
}

/**	получение карточки из базы	*/
void testGetCard()
{
	//int id;
	//DatabaseMonitor databaseMonitor;
	//LiteratureCard result = databaseMonitor.getCard(id);
	bool	bResult = true;
	
	DatabaseMonitor databaseMonitor;
	
	databaseMonitor.setDataBaseFileName(qstrDataBaseName);
	
	/**	список карточек	*/
	std::list<LiteratureCard>	lstCards;
	/**	итератор по списку карточек	*/
	std::list<LiteratureCard>::iterator	itCards;
	
	//	заполняем карточки
	for(int i = 0; i < 20; i++)
	{
		QString	qstrTmp = QString::number(i);
		LiteratureCard	card;

		card.setTitle(qstrTmp);
		card.setSphere(qstrTmp);
		card.setSource(qstrTmp);
		card.setPublicDate(QDate::currentDate());
		card.setFilePath(qstrTmp);
		card.setFileName(qstrTmp);
		card.setField(qstrTmp);
		card.setAuthor(qstrTmp);
		card.setAnnotation(qstrTmp);
		card.setAddDate(QDate::currentDate());

		lstCards.push_back(card);
	}
	
	//	заполняем карточки
	for(itCards = lstCards.begin(); itCards != lstCards.end(); itCards++)
	{
		try
		{
			//	передаем карточки для записи в базу данных
			bResult = databaseMonitor.writeCard(*itCards);
			if(!bResult)
			{
				std::cout << "can't write card" << std::endl;
			}
		}
		catch(...)
		{
			bResult = false;
			std::cout << "error work with DB" << std::endl;
		}
	}
	
	//	получаем карточку с номером
	int	id1 = 5;
	int	id2 = 125;
	LiteratureCard	readCard;
	
	//	карточка есть
	try
	{
		readCard = databaseMonitor.getCard(id1);
	}
	catch(int nEx)
	{
		//	ошибка
		std::cout << "card not found" << std::endl;
		bResult = false;
	}

	//	карточки нет
	try
	{
		readCard = databaseMonitor.getCard(id2);
	}
	catch(int nEx)
	{
		if(nEx == CARD_NOT_EXIST_EX)
			bResult = true;
		else
			bResult = false;
	}
	
	if(!bResult)
	{
		std::cout << "%TEST_FAILED% time=0 testname=testGetCard "
			"(databasemonitor_test) message=error message sample" 
			<< std::endl;
	}
}

void testSearch()
{
	bool	bResult = false;
	SearchParameter sp;
	sp = Title;
	LiteratureCard card;
	card.setTitle(QString("2"));
	DatabaseMonitor databaseMonitor;
	databaseMonitor.setDataBaseFileName(qstrDataBaseName);

	std::list<LiteratureCard> result = databaseMonitor.search(sp, card);

	if(result.size() > 0)
		bResult = true;

	if(!bResult)
	{
		std::cout << "%TEST_FAILED% time=0 testname=testSearch "
			"(databasemonitor_test) message=error message sample" 
			<< std::endl;
	}
}

/**	тест установки имени файла базы данных	*/
void testSetDataBaseFileName()
{
	DatabaseMonitor databaseMonitor;
	bool	bResult = true;

	try
	{
		databaseMonitor.setDataBaseFileName(qstrDataBaseName);
		bResult = true;
	}
	catch(int	nEx)
	{
		bResult = false;
		
		if(nEx == FILE_NOT_EXIST_EX)
			std::cout << "file not exist" << std::endl;
		if(nEx == CANT_OPEN_DATABASE)
			std::cout << "can't open DB" << std::endl;
	}
	if(!bResult)
	{
		std::cout << "%TEST_FAILED% time=0 testname=testSetDataBaseFileName "
			"(databasemonitor_test) message=error message sample" 
			<< std::endl;
	}
}

/**	тест записи литературных карточек в базу данных	*/
void testWriteCard()
{
	DatabaseMonitor databaseMonitor;
	databaseMonitor.setDataBaseFileName(qstrDataBaseName);

	bool	bResult = true;

	/**	список карточек	*/
	std::list<LiteratureCard>	lstCards;
	/**	итератор по списку карточек	*/
	std::list<LiteratureCard>::iterator	itCards;
	
	//	заполняем карточки
	for(int i = 0; i < 20; i++)
	{
		QString	qstrTmp = QString::number(i);
		LiteratureCard	card;

		card.setTitle(qstrTmp);
		card.setSphere(qstrTmp);
		card.setSource(qstrTmp);
		card.setPublicDate(QDate::currentDate());
		card.setFilePath(qstrTmp);
		card.setFileName(qstrTmp);
		card.setField(qstrTmp);
		card.setAuthor(qstrTmp);
		card.setAnnotation(qstrTmp);
		card.setAddDate(QDate::currentDate());

		lstCards.push_back(card);
	}
	
	//	заполняем карточки
	for(itCards = lstCards.begin(); itCards != lstCards.end(); itCards++)
	{
		//
		try
		{
			//	передаем карточки для записи в базу данных
			bResult = databaseMonitor.writeCard(*itCards);
			if(bResult == false)
			{
				std::cout << "can't write card" << std::endl;
			}
		}
		catch(...)
		{
			bResult = false;
			std::cout << "error DB" << std::endl;
		}
	}
	
	if(!bResult)
	{
		std::cout << "%TEST_FAILED% time=0 testname=testWriteCard "
			"(databasemonitor_test) message=error message sample" 
			<< std::endl;
	}
}

int main(int argc, char** argv)
{
	QApplication app(argc, argv);

	std::cout << "%SUITE_STARTING% databasemonitor_test" << std::endl;
	std::cout << "%SUITE_STARTED%" << std::endl;

	std::cout << "%TEST_STARTED% testCreateDataBase "
		"(databasemonitor_test)" 
		<< std::endl;
	testCreateDataBase();
	std::cout << "%TEST_FINISHED% time=0 testCreateDataBase "
		"(databasemonitor_test)" 
		<< std::endl;

	std::cout << "%TEST_STARTED% testSetDataBaseFileName (databasemonitor_test)"
		<< std::endl;
	testSetDataBaseFileName();
	std::cout << "%TEST_FINISHED% time=0 testSetDataBaseFileName "
		"(databasemonitor_test)"
		<< std::endl;

	std::cout << "%TEST_STARTED% testWriteCard (databasemonitor_test)"
		<< std::endl;
	testWriteCard();
	std::cout << "%TEST_FINISHED% time=0 testWriteCard (databasemonitor_test)"
		<< std::endl;

	std::cout << "%TEST_STARTED% testGetCard (databasemonitor_test)"
		<< std::endl;
	testGetCard();
	std::cout << "%TEST_FINISHED% time=0 testGetCard (databasemonitor_test)"
		<< std::endl;

	std::cout << "%TEST_STARTED% testSearch (databasemonitor_test)"
		<< std::endl;
	testSearch();
	std::cout << "%TEST_FINISHED% time=0 testSearch (databasemonitor_test)"
		<< std::endl;

	std::cout << "%SUITE_FINISHED% time=0" << std::endl;

	QWidget	wgt;
	wgt.show();

	return app.exec();
}

